package org.example.controller;

import org.example.constant.HttpConstants;
import org.example.entity.UserEntity;
import org.example.exception.UserException;
import org.example.service.IUserService;
import org.example.util.ResponseEntity;
import org.example.util.TokenHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author hanjiafeng
 * @date 2021/4/19 17:30
 * @desc 登录控制器
 */
@RestController
@RequestMapping("user")
public class UserController {

    private final TokenHelper tokenHelper;

    private final IUserService userService;

    public UserController(IUserService userService, TokenHelper tokenHelper) {
        this.userService = userService;
        this.tokenHelper = tokenHelper;
    }

    @PostMapping("login")
    public ResponseEntity<Void> login(String username, String password, HttpServletResponse httpServletResponse) {
        ResponseEntity<Void> responseEntity;
        try {
            UserEntity userEntity = userService.login(username, password);
            //生成token并保存到redis
            String accessToken = tokenHelper.createToken(userEntity);
            //将token返回客户端
            httpServletResponse.setHeader(HttpConstants.ACCESS_TOKEN, accessToken);
            responseEntity = ResponseEntity.success("登陆成功");
        } catch (UserException e) {
            e.printStackTrace();
            responseEntity = ResponseEntity.failure("登陆失败:" + e.getMessage());
        }
        return responseEntity;
    }

    @GetMapping("checkUsername")
    public ResponseEntity<Void> checkUsername(String username) {
        ResponseEntity<Void> responseEntity;
        try {
            boolean isRepeat = userService.checkUsername(username);
            if (isRepeat) {
                responseEntity = ResponseEntity.success("true");
            } else {
                responseEntity = ResponseEntity.success("false");
            }
        } catch (UserException e) {
            e.printStackTrace();
            responseEntity = ResponseEntity.failure("检查用户名失败:" + e.getMessage());
        }
        return responseEntity;
    }

    @PostMapping("register")
    public ResponseEntity<Void> register(String username, String password) {
        ResponseEntity<Void> responseEntity;
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        try {
            int count = userService.regist(userEntity);
            if (count == 0) {
                responseEntity = ResponseEntity.success("注册成功");
            } else {
                throw new UserException("系统异常");
            }
        } catch (UserException e) {
            e.printStackTrace();
            responseEntity = ResponseEntity.failure("注册失败:" + e.getMessage());
        }
        return responseEntity;
    }
}
